package com.dtw.ditouwang.ui.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommonBean;
import com.dtw.ditouwang.ui.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForgetPasswordActivity extends BaseActivity implements View.OnClickListener {

    /**
     * 请输入11位手机号
     */
    private EditText editPhone;
    /**
     * 请输入验证码
     */
    private EditText editCode;
    /**
     * 获取验证码
     */
    private TextView textGetCode;
    /**
     * 请输入新密码
     */
    private EditText editPass;
    /**
     * 请确认密码
     */
    private EditText editPassAgain;
    /**
     * 登录
     */
    private Button buttonLogin;
    CountDownTimer countDownTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_froget_pass);
        initView();
        enableBack();
        setTitle("忘记密码");
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                textGetCode.setText(millisUntilFinished / 1000 + " s");
            }

            @Override
            public void onFinish() {
                textGetCode.setText("获取验证码");
                textGetCode.setEnabled(true);
            }
        };

    }

    private void initView() {
        editPhone = findViewById(R.id.edit_phone);
        editCode = findViewById(R.id.edit_code);
        textGetCode = findViewById(R.id.text_get_code);
        editPass = findViewById(R.id.edit_pass);
        editPassAgain = findViewById(R.id.edit_pass_again);
        buttonLogin = findViewById(R.id.button_login);
        buttonLogin.setOnClickListener(this);
        textGetCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.button_login:
                if(!TextUtils.isEmpty(editPass.getText().toString())&&editPass.getText().toString().equals(editPassAgain.getText().toString())) {
                    webApi.forgetPassword(editPhone.getText().toString(),editPass.getText().toString(),editCode.getText().toString()).enqueue(new Callback<CommonBean>() {
                        @Override
                        public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                            if(response.body().isResult()){
                                showToast("密码修改成功");
                                finish();
                            }else{
                                showToast(response.body().getMsg());
                            }
                        }

                        @Override
                        public void onFailure(Call<CommonBean> call, Throwable t) {

                        }
                    });
                }else{
                    showToast("密码不能为空，且两次密码必须一致");
                }
                break;
            case R.id.text_get_code:
                countDownTimer.start();
                textGetCode.setEnabled(false);
                webApi.getCode(editPhone.getText().toString(), 3).enqueue(new Callback<CommonBean>() {
                    @Override
                    public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                        if (response.body().isResult()) {
                            showToast("验证码发送成功");
                        } else {
                            showToast(response.body().getMsg());
                            countDownTimer.cancel();
                            textGetCode.setText("获取验证码");
                            textGetCode.setEnabled(true);
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonBean> call, Throwable t) {

                    }
                });
                break;
        }
    }
}
